package io.renren.modules.gaokaobaobei.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.gaokaobaobei.entity.ZhaoShengEntity;
import io.renren.modules.gaokaobaobei.service.AgencyService;
import io.renren.modules.gaokaobaobei.service.ZhaoShengService;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;



/**
 * 志愿设计管理
 *
 * @author zlb
 * @email 316682546@qq.com
 * @date 2018-10-30 09:55:16
 */
@RestController
@RequestMapping("gaokaobaobei/zhaosheng")
public class ZhaoShengController {
    @Autowired
    private ZhaoShengService zhaoShengService;
    @Autowired
    private AgencyService agencyService;


    /**
     *  点击审核 记录 数据及状态
     * @return
     */
    @RequestMapping("/baobei")
    public R baobei(String beizhu,String number){
        Map<String,String> map = new HashMap<String,String>();
        map.put("school",beizhu);//此为备注
        map.put("number",number);
        map.put("type","等待审核");
        //通过考号来更新状态
        boolean flag = zhaoShengService.updateBeiZhuByUserName(map);
        return R.ok().put("flag",flag);
    }

    /**
     * 我的业绩列表
     */
    @RequestMapping("/myOutstandinglist")
    public R myOutstandinglist(@RequestParam Map<String, Object> params){
        String type="审核通过";
        PageUtils page = zhaoShengService.queryPageAdmissionstype(params,(String) params.get("Admissionstype"),type,(String)params.get("name"));
        return R.ok().put("page",page);
    }
    /**
     * 代理夏考业绩列表
     */
    @RequestMapping("/listXiakao")
    public R listXiakao(@RequestParam Map<String, Object> params){
        String type="审核通过";
        String Admissionstype = "夏考";
        PageUtils page = zhaoShengService.queryPageAdmissionstype(params,Admissionstype,type,(String)params.get("name"));
        return R.ok().put("page",page);
    }
    /**
     * 代理单招业绩列表
     */
    @RequestMapping("/listDanzhao")
    public R listDanzhao(@RequestParam Map<String, Object> params){
        String type="审核通过";
        String Admissionstype = "单招";
        PageUtils page = zhaoShengService.queryPageAdmissionstype(params,Admissionstype,type,(String)params.get("name"));
        return R.ok().put("page",page);
    }
    /**
     * 代理升本业绩列表
     */
    @RequestMapping("/listShengben")
    public R listShengben(@RequestParam Map<String, Object> params){
        String type="审核通过";
        String Admissionstype = "升本";
        PageUtils page = zhaoShengService.queryPageAdmissionstype(params,Admissionstype,type,(String)params.get("name"));
        return R.ok().put("page",page);
    }
    /**
     * 代理考研业绩列表
     */
    @RequestMapping("/listKaoyan")
    public R listKaoyan(@RequestParam Map<String, Object> params){
        String type="审核通过";
        String Admissionstype = "考研";
        PageUtils page = zhaoShengService.queryPageAdmissionstype(params,Admissionstype,type,(String)params.get("name"));
        return R.ok().put("page",page);
    }

    /**
     * 夏考列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, String type, HttpSession session){
        String name = (String) session.getAttribute("name");
        String Admissionstype = "";
        PageUtils page = zhaoShengService.queryPage(params,type,"夏考",name);
        session.removeAttribute("name");
        return R.ok().put("page", page);
    }
    /**
     * 单招列表
     */
    @RequestMapping("/list2")
    public R list2(@RequestParam Map<String, Object> params, String type,HttpSession session){
        String name = (String) session.getAttribute("name");
        String Admissionstype = "";
        PageUtils page = zhaoShengService.queryPage(params,type,"单招",name);
        session.removeAttribute("name");
        return R.ok().put("page", page);
    }

    /**
     * 升本列表
     */
    @RequestMapping("/listshengben")
    public R listshengben(@RequestParam Map<String, Object> params, String type,HttpSession session){
        String name = (String) session.getAttribute("name");
        String Admissionstype = "";
        PageUtils page = zhaoShengService.queryPage(params,type,"升本",name);
        session.removeAttribute("name");
        return R.ok().put("page", page);
    }


    /**
     * 考研列表
     */
    @RequestMapping("/listkaoyan")
    public R listkaoyan(@RequestParam Map<String, Object> params, String type,HttpSession session){
        String name = (String) session.getAttribute("name");
        String Admissionstype = "";
        PageUtils page = zhaoShengService.queryPage(params,type,"考研",name);
        session.removeAttribute("name");
        return R.ok().put("page", page);
    }

    /**
     * 代理人业绩
     */
    @RequestMapping("/PerformanceAudit")
    public R PerformanceAudit(@RequestParam Map<String, Object> params,HttpSession session,String type){
        if(type==null){
            type = "等待审核";

        }
        String name = (String) session.getAttribute("name");//招生人员姓名
        int typeINT = (int) session.getAttribute("type");// 招生类型
        String Admissionstype = typeINT==0?"夏考":"单招";
        PageUtils page = zhaoShengService.PerformanceAudit(params,Admissionstype,name,type);

//        session.removeAttribute("name");
//        session.removeAttribute("type");
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("gaokaobaobei:zhaosheng:info")
    public R info(@PathVariable("id") Integer id){
        ZhaoShengEntity userBackup = zhaoShengService.selectById(id);

        return R.ok().put("userBackup", userBackup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody  ZhaoShengEntity userBackup){
        if(userBackup.getAdmissionstype().equals("单招")){
            if(userBackup.getName()==null
//                    ||userBackup.getCandidateNumber()==null
//                    ||userBackup.getScore()==null
                    ||userBackup.getAdmissionstype()==null
                    ||userBackup.getHighSchool()==null
                    ||userBackup.getPhone()==null){
                return R.error("请完善你的信息！");
            }
        }else{
            if(userBackup.getName()==null
                    ||userBackup.getCandidateNumber()==null
                    ||userBackup.getWlProperty()==null
                    ||userBackup.getScore()==null
                    ||userBackup.getAdmissionstype()==null
                    ||userBackup.getHighSchool()==null
                    ||userBackup.getPhone()==null){
                return R.error("请完善你的信息！");
            }
        }
        String username = ShiroUtils.getSessionAttribute("username")+"";
        Long userId = ShiroUtils.getUserId();
        userBackup.setUsername(username);
        userBackup.setUserId(userId);
        zhaoShengService.insert(userBackup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody  ZhaoShengEntity userBackup){
        ValidatorUtils.validateEntity(userBackup);
        zhaoShengService.updateAllColumnById(userBackup);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        zhaoShengService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     *
     * 通过点击审核 和拒绝来 执行 添加到代理人业绩
     * @param yuanyin  拒绝的原因
     * @param id  该条志愿的id
     * @param num  通过num 来判断是同意 还是拒绝
     */
    @RequestMapping("/updateAgency")
    public R updateAgency(String yuanyin ,String num,int id){
        Map<String ,Object> map = new HashMap();
        //拒绝
        if(num.equals("0")){
            map.put("id",id);
            map.put("major",yuanyin);
            map.put("type","审核未通过");
            zhaoShengService.updateAgency(map);
        }else{
            //同意的操作
            Map<String ,String>  map2  = new HashMap();
            //查询 业绩条数
            int number =   agencyService.selectNumber(map2);
            map.put("id",id);
            map.put("major","");
            map.put("type","审核通过");
            //更新志愿 状态为审核通过
            zhaoShengService.updateAgency(map);
            //获取该条信息的 发布人及时夏考还是单招
            ZhaoShengEntity entity = zhaoShengService.findUserById(id);
            map2.put("username",entity.getUsername());
            map2.put("Admissionstype",entity.getAdmissionstype());
            //username,Admissionstype
            number++;
            map2.put("number",number+"");
            //username,Admissionstype,number  更新代理人业绩
            agencyService.updateNumber(map2);
        }
        return R.ok().put("flag",true);

    }

}
